package pers.xuehongyan.useat.server.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Component;
import pers.xuehongyan.useat.server.enity.Seat;
import pers.xuehongyan.useat.server.exception.seat.SeatNotFoundException;
import pers.xuehongyan.useat.server.mapper.SeatMapper;

import javax.annotation.Resource;

/**
 * @author xuehongyan
 */
@Component
public class SeatManager {
    @Resource
    SeatMapper seatMapper;

    public void update(Seat seat) {
        seatMapper.updateById(seat);
    }

    public Seat getOneSeatByMachineId(Integer machineId) throws SeatNotFoundException {
        Seat seat = seatMapper.selectOne(new QueryWrapper<Seat>().eq("machine_id", machineId));
        if(seat == null) {
            throw new SeatNotFoundException();
        }
        return seat;
    }

    public Seat getSeatById(Integer id) throws SeatNotFoundException {
        Seat seat = seatMapper.selectById(id);
        if(seat == null) {
            throw new SeatNotFoundException();
        }
        return seat;
    }

    public void create(Seat seat) {
        seatMapper.insert(seat);
    }
}
